//
//  ST_MeetYouSegmentControl.swift
//  btvideo
//
//  Created by mac on 2020/8/3.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

class ST_MeetYouSegmentControl: UIView {
    
    static let lineHeight = 4.cgFloat
    
    static let lineWidth = 18.cgFloat
    
    static let height = 44
    
    var space = 30.cgFloat
    
    var selectedTextColor = UIColor.init(hex: 0x333333)
    
    var normalTextColor = UIColor.init(hex: 0x4C4C4C)
    
    var textNormalFont = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.regular)
    
    var textSelectedFont = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
    
    
    fileprivate let red_point_width:CGFloat = 10
    
    fileprivate lazy var redPoint0:UIView = {
        let v = UIView()
        v.alpha = 0
        v.backgroundColor = UIColor.red
        v.cornerRadius = red_point_width/2.0
        return v
    }()
    
    fileprivate lazy var redPoint1:UIView = {
        let v = UIView()
        v.alpha = 0
        v.backgroundColor = UIColor.red
        v.cornerRadius = red_point_width/2.0
        return v
    }()
    
//    fileprivate lazy var redPointViews = [redPoint0,redPoint1]
    
    func show_red_point(index:Int){
        
//        redPointViews[safe:index]?.alpha = 1
    }
    
    func hidden_red_point(index:Int){
//        redPointViews[safe:index]?.alpha = 0
    }
    
    var titleArray = [String](){
        didSet{
            
            self.removeSubviews()
            
            let itemBtn = UIButton.init(type: UIButton.ButtonType.custom)
        
            if let str = titleArray.first{
                itemBtn.setTitleForAllStates(str)
            }
            itemBtn.setTitleColor(selectedTextColor, for: UIControl.State.selected)
            itemBtn.setTitleColor(normalTextColor, for: UIControl.State.normal)
            itemBtn.titleLabel?.font = textNormalFont
            self.addSubview(itemBtn)
            
            itemBtn.snp.makeConstraints { (make) in
                make.right.equalTo(self.snp.centerX).offset(-TIANCompatibleValue(value: 75/2.0))
                make.top.bottom.equalToSuperview()
            }
            itemBtn.addTarget(self, action: #selector(click_handle(sender:)), for: UIControl.Event.touchUpInside)
            
            itemBtn.tag = baseTag
            itemBtn.titleLabel?.font = textSelectedFont
            itemBtn.isSelected = true
            last_selected_btn = itemBtn
        
            
            itemBtn.addSubview(redPoint0)
            redPoint0.snp.makeConstraints { (make) in
                make.height.width.equalTo(red_point_width)
                make.top.equalToSuperview().offset(3)
                make.right.equalToSuperview().offset(-19/2)
            }
        
        
            
        
            
            let rightitemBtn = UIButton.init(type: UIButton.ButtonType.custom)
            
            if let str = titleArray[safe:1]{
                rightitemBtn.setTitleForAllStates(str)
            }
            rightitemBtn.setTitleColor(selectedTextColor, for: UIControl.State.selected)
            rightitemBtn.setTitleColor(normalTextColor, for: UIControl.State.normal)
            rightitemBtn.titleLabel?.font = textNormalFont
            self.addSubview(rightitemBtn)
            
            rightitemBtn.snp.makeConstraints { (make) in
                make.left.equalTo(self.snp.centerX).offset(TIANCompatibleValue(value: 75/2.0))
                make.top.bottom.equalToSuperview()
            }
            rightitemBtn.addTarget(self, action: #selector(click_handle(sender:)), for: UIControl.Event.touchUpInside)
            rightitemBtn.tag = baseTag + 1
            
            rightitemBtn.addSubview(redPoint0)
            redPoint0.snp.makeConstraints { (make) in
                make.height.width.equalTo(red_point_width)
                make.top.equalToSuperview().offset(3)
                make.right.equalToSuperview().offset(-19/2)
            }
            
        
            self.addSubview(line)
            line.snp.makeConstraints { (make) in
                make.width.equalTo(ST_GitSegmentControl.lineWidth)
                make.height.equalTo(ST_GitSegmentControl.lineHeight)
                make.centerX.equalTo(itemBtn)
                make.bottom.equalToSuperview()
            }
                
            
        }
    }
    
    fileprivate var last_selected_btn:UIButton?
    
    fileprivate let baseTag = 1237
    
    fileprivate lazy var line:UIView = {
       let control = UIView()
        control.backgroundColor = UIColor.init(hex: 0xFEC207)
        control.cornerRadius = 1
        return control
    }()
    
    
    var selected_closure:((Int)->())?
}

//MARK:- Event
extension ST_MeetYouSegmentControl{
    
    func selectBtn(index:Int){
        
        let find = self.subviews.first { (v) -> Bool in
            return v != line && v.tag == self.baseTag + index
        }
        
        if let btn = find as? UIButton{
            self.clickedLayout(sender: btn)
        }
    }
    fileprivate func clickedLayout(sender:UIButton){
        
        if sender.isSelected{
            return
        }
        
        line.snp.remakeConstraints { (make) in
            make.width.equalTo(ST_GitSegmentControl.lineWidth)
            make.height.equalTo(ST_GitSegmentControl.lineHeight)
            make.centerX.equalTo(sender)
            make.bottom.equalToSuperview()
        }
        
        UIView.animate(withDuration: 0.25) {
            self.layoutIfNeeded()
        }
        
        sender.titleLabel?.font = textSelectedFont
        last_selected_btn?.titleLabel?.font = textNormalFont
        
        last_selected_btn?.isSelected = false
        sender.isSelected = true
        last_selected_btn = sender
        
    }
    
    @objc fileprivate func click_handle(sender:UIButton){
        
        if sender.isSelected{
            return
        }
        self.clickedLayout(sender: sender)
        
        self.selected_closure?(sender.tag - self.baseTag)
        
    }
    
}
